home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / lib / glut / strokegen.y < prev    next >
Encoding:
Lex Description  |  1996-11-11  |  15.4 KB  |  650 lines

  1. %{
  2. /* $XConsortium: to_wfont.y,v 5.7 94/04/17 20:10:08 rws Exp $ */
  3.  
  4. /*****************************************************************
  5.  
  6. Copyright (c) 1989,1990, 1991  X Consortium
  7.  
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14.  
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17.  
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  21. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24.  
  25. Except as contained in this notice, the name of the X Consortium shall not be
  26. used in advertising or otherwise to promote the sale, use or other dealings
  27. in this Software without prior written authorization from the X Consortium.
  28.  
  29. Copyright (c) 1989,1990, 1991 by Sun Microsystems, Inc.
  30.  
  31.                         All Rights Reserved
  32.  
  33. Permission to use, copy, modify, and distribute this software and its 
  34. documentation for any purpose and without fee is hereby granted, 
  35. provided that the above copyright notice appear in all copies and that
  36. both that copyright notice and this permission notice appear in 
  37. supporting documentation, and that the names of Sun Microsystems,
  38. and the X Consortium, not be used in advertising or publicity 
  39. pertaining to distribution of the software without specific, written 
  40. prior permission.  
  41.  
  42. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
  43. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT 
  44. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
  45. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  46. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  47. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  48. SOFTWARE.
  49.  
  50. ******************************************************************/
  51.  
  52.  
  53. #define YYMAXDEPTH 10000
  54.  
  55. #include <X11/Xos.h>
  56. #include <stdio.h>
  57. #include <ctype.h>
  58. #ifndef L_SET
  59. #define L_SET SEEK_SET
  60. #endif
  61. #include "stroke.h"
  62.  
  63. #ifdef X_NOT_STDC_ENV
  64. FILE *fopen();
  65. #endif
  66.  
  67. typedef struct {
  68.  
  69.         float           std_left,      /* NCGA standard left spacing */
  70.                         std_wide,      /* character width            */  
  71.                         std_rght;      /* Right spacing              */  
  72. }               Standard;
  73.  
  74.  
  75. char            fname[80];
  76. char            symname[80] = "FONT";
  77. Dispatch        *Table;    /* dispatch table */
  78. Standard    *sp_table; /* NCGA font spacings */
  79. Path            *strokes;  /* strokes of each character */
  80. Property        *property; /* property list */
  81.  
  82. struct {
  83.     int path, point, props;
  84. } count, expect;
  85.  
  86. Path_subpath   *current_path;
  87.  
  88. Font_header     head;        /* font header */
  89. int             tableindex;    /* which character */
  90. int             yyerrno;    /* error number */
  91.  
  92. %}
  93.  
  94. %union {
  95.     int    nil;    /* void is reserved */
  96.     int    ival;
  97.     float    dval;
  98.     char    *cval;
  99. }
  100.  
  101. %start font
  102.  
  103. %token <dval> REAL
  104. %token <ival> INTEGER
  105. %token <cval> STRING
  106.  
  107. %token <nil> BOTTOM
  108. %token <nil> CENTER
  109. %token <nil> CLOSE
  110. %token <nil> FONTNAME
  111. %token <nil> PROPERTIES
  112. %token <nil> NUM_CH
  113. %token <nil> INDEX
  114. %token <nil> L_SPACE
  115. %token <nil> MAGIC
  116. %token <nil> OPEN
  117. %token <nil> RIGHT
  118. %token <nil> R_SPACE
  119. %token <nil> STROKE
  120. %token <nil> TOP
  121. %token <nil> VERTICES
  122. %token <nil> BEARING
  123. %token <nil> WIDTH
  124.  
  125. %type <cval> fontname
  126. %type <ival> properties
  127. %type <dval> top bottom center right
  128. %type <ival> nstroke nvertice n_pts index num_ch
  129. %type <ival> closeflag
  130. %type <ival> counter
  131. %type <dval> sp_left wide sp_right
  132.  
  133. %%
  134.  
  135. font : fontheader num_ch fontprops fontbody spacing { fini(); }|
  136.     fontheader fontbody  { fini(); };
  137.  
  138. fontheader : fontname top bottom 
  139.     { wf_header($1, $2, $3); };
  140.  
  141. fontname : FONTNAME STRING
  142.     { $$ = $2; };
  143.  
  144. top : TOP REAL { $$ = $2;};
  145.  
  146. bottom : BOTTOM REAL { $$ = $2;};
  147.  
  148. num_ch: NUM_CH INTEGER { set_num_ch($2);};
  149.  
  150. fontprops : /* empty */ | properties;
  151.  
  152. properties : PROPERTIES INTEGER { init_properties ($2); } property_list
  153.         { check_num_props (); }
  154.  
  155. property_list : /* empty */ | single_property property_list
  156.  
  157. single_property : STRING STRING { add_property($1, $2); };
  158.  
  159. fontbody :     /* empty */ 
  160.     | glyph fontbody;
  161.  
  162. glyph : glyph_header strokes
  163.     { check_nstroke(); };
  164.  
  165. glyph_header : index { tableindex = $1; } sym_headinfo;
  166.  
  167. sym_headinfo :  nstroke center right nvertice
  168.     { glyph_header($1, $2, $3, $4); };
  169.  
  170. index : INDEX INTEGER { check_num_ch(); $$ = $2; };
  171.  
  172. nstroke : STROKE INTEGER { $$ = $2; expect.path = $2; };
  173.  
  174. nvertice: {$$ = 0;} | VERTICES INTEGER  { $$ = $2; }  ;
  175.  
  176. center : CENTER REAL{ $$ = $2; };
  177.  
  178. right : RIGHT REAL{ $$ = $2; };
  179.  
  180. strokes :    /* empty */ | path strokes;
  181.  
  182. path : closeflag n_pts { init_path($1, $2); } points
  183.     { check_npts(); }
  184.  
  185. points :     /* empty */ | coord points;
  186.  
  187. closeflag : CLOSE { $$ = $1 == CLOSE; } | OPEN { $$ = $1 == CLOSE; } ;
  188.  
  189. n_pts : INTEGER { $$ = $1; };
  190.  
  191. coord : REAL REAL { add_point($1, $2); };
  192.  
  193. spacing :     /* empty */ 
  194.     | item spacing;
  195.  
  196. item : counter {tableindex = $1;} sp_left wide sp_right
  197.     { std_space($3, $4, $5); };
  198.  
  199. counter  : BEARING INTEGER {$$ = $2;};
  200.  
  201. sp_left: L_SPACE REAL {$$ = $2;};
  202.  
  203. wide :  WIDTH REAL {$$ = $2;};
  204.  
  205. sp_right: R_SPACE REAL {$$ = $2;};
  206.  
  207. %%
  208.  
  209. #define BYE(err)    yyerrno = (err), yyerror()
  210.  
  211. #define ERR_BASE    1000
  212. #define OPEN_ERROR     1001
  213. #define WRITE_ERROR     1002
  214. #define WRONG_NAME     1003
  215. #define NO_MEMORY     1004
  216. #define EXCEED_PATH     1005
  217. #define EXCEED_POINT     1006
  218. #define PATH_MISMATCH    1007
  219. #define POINT_MISMATCH    1008
  220. #define PROP_MISMATCH   1009
  221. #define EXCEED_PROPS     1010
  222.  
  223. char    *prog;
  224.  
  225. main(argc, argv)
  226.     int             argc;
  227.     char           *argv[];
  228.  
  229. {
  230.     /* Usage : to_wfont [-o outfile] [infile] */
  231.     char           *s;
  232.  
  233.     fname[0] = 0;
  234.     tableindex = 0;
  235.     head.num_ch = -1;
  236.  
  237.     prog = *argv;
  238.     while (--argc > 0 && *++argv != NULL) {
  239.         s = *argv;
  240.         if (*s++ == '-')
  241.             switch (*s) {
  242.             case 's':
  243.                 if (*++argv != NULL)
  244.                 {
  245.                     --argc;
  246.                     (void) strcpy(symname, *argv);
  247.                 }
  248.                 break;
  249.             case 'o':
  250.                 if (*++argv != NULL)
  251.                 {
  252.                     --argc;
  253.                     (void) strcpy(fname, *argv);
  254.                 }
  255.                 break;
  256.             default:      /* ignore other options */
  257.                 ;
  258.             }
  259.         else {
  260.             FILE           *fp;
  261.  
  262.             /* standard input redirection */
  263.             fp = fopen(*argv, "r");
  264.             if (fp != NULL) {
  265.                 if (close(fileno(stdin)) < 0)
  266.                 {
  267.                     perror(prog);
  268.                     return;
  269.                 }
  270.                 if (dup(fileno(fp)) < 0)
  271.                 {
  272.                     perror(prog);
  273.                     return;
  274.                 }
  275.                 (void) fclose(fp);
  276.             }
  277.         }
  278.     }
  279.     return (yyparse());
  280. }
  281.  
  282. /* set number of characters */
  283. set_num_ch(num_ch)
  284. int num_ch;
  285. {
  286.     yyerrno = 0;
  287.     head.num_ch = num_ch;
  288.     if (num_ch > 0) {
  289.       Table    = (Dispatch *) malloc(num_ch * sizeof(Dispatch));
  290.       sp_table = (Standard *) malloc(num_ch * sizeof(Standard));
  291.       strokes  = (Path *)     malloc(num_ch * sizeof(Path));
  292.     }
  293.  
  294.     for (tableindex = 0; tableindex < num_ch; tableindex++) {
  295.         Table[tableindex].center = 0.0;
  296.         Table[tableindex].right = 0.0;
  297.         Table[tableindex].offset = 0;
  298.  
  299.         sp_table[tableindex].std_left = 0.0;
  300.         sp_table[tableindex].std_wide = 0.0;
  301.         sp_table[tableindex].std_rght = 0.0;
  302.  
  303.         strokes[tableindex].n_subpaths = 0;
  304.         strokes[tableindex].n_vertices = 0;
  305.         strokes[tableindex].subpaths = NULL;
  306.     }
  307. }
  308.  
  309. /* initialize the property info in the header */
  310. init_properties(num_props)
  311.     int             num_props;
  312. {
  313.     if (num_props > 0) {
  314.       head.properties = (Property *) 
  315.                           malloc (num_props * sizeof (Property));
  316.       head.num_props = expect.props = num_props;
  317.     }
  318.     else {
  319.       head.properties = NULL;
  320.       head.num_props = expect.props = 0;
  321.     }
  322.     count.props = -1;
  323.     property = head.properties;  /* initialize the list pointer */
  324. }
  325.  
  326. check_num_props()
  327. {
  328.         count.props++;
  329.         if (count.props != expect.props)
  330.       BYE (PROP_MISMATCH);
  331. }
  332.  
  333. /* add individual property info into the buffer */
  334. add_property(name, value)
  335.     char            *name,
  336.             *value;
  337. {
  338.         /* check if the property exceeds allocated space */
  339.  
  340.         if (++count.props >= head.num_props)
  341.          BYE(EXCEED_PROPS);
  342.  
  343.     /* copy the strings into the buffer */
  344.  
  345.     (void) strcpy (property->propname, name);
  346.     (void) strcpy (property->propvalue, value);
  347.  
  348.     /* increment the property pointer */
  349.  
  350.     property++;
  351. }
  352.  
  353. check_num_ch()
  354. {
  355.  
  356.   if (head.num_ch == -1)
  357.     set_num_ch(128);
  358.  
  359. }
  360.  
  361. yyerror()
  362. {
  363. #ifndef __bsdi__
  364.     extern int      yylineno;
  365. #endif
  366. #    define ERR_SIZE (sizeof(err_string) / sizeof(char *))
  367.     static char    *err_string[] = {
  368.         "Cannot open file",
  369.         "Write fails",
  370.         "Illegal font name",
  371.         "Memory allocation failure",
  372.         "Specified number of strokes exceeded",
  373.         "Specified number of points exceeded",
  374.         "Number of strokes do not match",
  375.         "Number of points do not match",
  376.         "Number of properties do not match",
  377.         "Specified number of properties exceeded",
  378.     0};
  379.     char           *str;
  380.  
  381.     yyerrno -= ERR_BASE;
  382.     if (yyerrno > 0 && yyerrno < ERR_SIZE)
  383.         str = err_string[yyerrno-1];
  384.     else
  385.         str = "Syntax error";
  386. #ifdef __bsdi__
  387.         fprintf(stderr, "%s.\n", str);
  388. #else
  389.         fprintf(stderr, "line %d: %s.\n", yylineno, str);
  390. #endif
  391.     freeall();
  392.     (void) unlink(fname);
  393.     exit(1);
  394. }
  395.  
  396. /* create wfont header */
  397. wf_header(name, top, bottom)
  398.     char           *name;
  399.     float           top,
  400.                     bottom;
  401. {
  402.  
  403.     if (name == NULL)
  404.         BYE(WRONG_NAME);
  405.     head.top = (float) top;
  406.     head.bottom = (float) bottom;
  407.     head.magic = WFONT_MAGIC_PEX;
  408.     (void) strcpy(head.name, name);
  409.     free(name);
  410. }
  411.  
  412. /* create header for each glyph */
  413. glyph_header(npath, center, right, npts)
  414.     int             npath,
  415.                     npts;
  416.     float           center,
  417.                     right;
  418. {
  419.      {
  420.     register Path  *strk = strokes + tableindex;
  421.     
  422.         if (npath > 0)     /* Don't allocate space unless the character
  423.                   has strokes associated with it. */
  424.     {
  425.         strk->subpaths = (Path_subpath *)
  426.             malloc(npath * sizeof (Path_subpath));
  427.  
  428.         if (strk->subpaths == NULL)
  429.             BYE(NO_MEMORY);
  430.  
  431.         strk->type = PATH_2DF;
  432.         strk->n_subpaths = npath;
  433.         strk->n_vertices = npts;
  434.     }
  435.     else {            /* Just initialize the entry */
  436.             strk->subpaths = NULL;
  437.         strk->type = PATH_2DF;
  438.         strk->n_subpaths = 0;
  439.         strk->n_vertices = 0;
  440.     }
  441.       }
  442.       {
  443.         register Dispatch *tbl = Table + tableindex;
  444.  
  445.         tbl->offset = 0;
  446.         tbl->center = center;
  447.         tbl->right = right;
  448.       }
  449.     count.path = -1;           /* initialize path counter, not to
  450.                         * exceed n_subpath */
  451. }
  452.  
  453. /* create standard spacing info for each glyph  */
  454. std_space(l_bear, wide, r_bear)
  455.  
  456.     float l_bear,
  457.           wide,
  458.           r_bear;
  459. {
  460.     register Standard *tbl = sp_table +tableindex;
  461.     tbl->std_left = l_bear;
  462.     tbl->std_wide = wide;
  463.     tbl->std_rght = r_bear;
  464. }
  465.  
  466. /* initialize each sub_path */
  467. init_path(close, n)
  468.     int             close,
  469.                     n;
  470. {
  471.     register Path_subpath *path;
  472.  
  473.     if (++count.path >= strokes[tableindex].n_subpaths)
  474.         BYE(EXCEED_PATH);
  475.     path = current_path = strokes[tableindex].subpaths + count.path;
  476.     path->n_pts = n;
  477.     path->closed = close;
  478.     if (n > 0) 
  479.       path->pts.pt2df = (Path_point2df *) 
  480.                          malloc(n * sizeof (Path_point2df));
  481.     if (path->pts.pt2df == NULL)
  482.         BYE(NO_MEMORY);
  483.     expect.point = path->n_pts;
  484.     count.point = -1;           /* initialize point counter, not to
  485.                         * exceed n_pts */
  486. }
  487.  
  488. /* accumulating points for each sub_path */
  489. add_point(x, y)
  490.     float           x,
  491.                     y;
  492. {
  493.     register Path_subpath   *path;
  494.     register Path_point2df    *pt_ptr;
  495.  
  496.     path = current_path;
  497.     if (++count.point >= path->n_pts)
  498.         BYE(EXCEED_POINT);
  499.     pt_ptr = path->pts.pt2df + count.point;
  500.     pt_ptr->x = x;
  501.     pt_ptr->y = y;
  502. }
  503.  
  504. /* Path_type + n_subpaths + n_vertices */
  505. #define STROKE_HEAD (sizeof (Path_type) + sizeof (int) + sizeof (int))
  506.  
  507. /* write out file, close everything, free everything */
  508. fini()
  509. {
  510.     static long     zero = 0;
  511.  
  512.     /* pointers used to walk the arrays */
  513.     register Path_subpath *spath;
  514.     register Dispatch *tbl_ptr;
  515.     register Path  *strptr;
  516.     register Property *prop_ptr;
  517.  
  518.     FILE           *fp;
  519.     int             npath;
  520.     register int    i,
  521.                     j,
  522.             k;
  523.     Standard    *sp_ptr;
  524.     Path_point2df    *pt;
  525.  
  526.         printf("\n/* GENERATED FILE -- DO NOT MODIFY */\n\n");
  527.         printf("#include \"glutstroke.h\"\n\n");
  528.  
  529. #    define BY_BYE(err) fclose(fp), BYE(err)
  530.  
  531.     /* adjust the characters for spacing, note max char width */
  532.     head.max_width = 0.0;
  533.     for (i = 0, tbl_ptr = Table, strptr = strokes, sp_ptr = sp_table;
  534.              i < head.num_ch; i++, tbl_ptr++, strptr++, sp_ptr++) {
  535.         tbl_ptr->center += sp_ptr->std_left;
  536.         tbl_ptr->right += sp_ptr->std_left + sp_ptr->std_rght;
  537.         if (tbl_ptr->right > head.max_width)
  538.             head.max_width = tbl_ptr->right;
  539.         npath = strptr->n_subpaths;
  540.         if (npath > 0 || tbl_ptr->center != 0.0 ||
  541.                     tbl_ptr->right != 0.0) {
  542.             for (j = 0, spath = strptr->subpaths;
  543.                              j < npath; j++, spath++) {
  544.                 for(k=0, pt = spath->pts.pt2df;
  545.                      k<spath->n_pts; k++, pt++) {
  546.                     pt->x += sp_ptr->std_left;
  547.                 }
  548.             }
  549.         }
  550.     }
  551.  
  552.     /* write the stroke table */
  553.     for (i = 0, tbl_ptr = Table, strptr = strokes;
  554.          i < head.num_ch; i++, tbl_ptr++, strptr++) {
  555.         if (strptr->n_subpaths > 0 &&
  556.             tbl_ptr->center != 0.0 &&
  557.             tbl_ptr->right != 0.0) {
  558.             if(isprint(i)) {
  559.                 printf("/* char: %d '%c' */\n\n", i, i);
  560.             } else {
  561.                 printf("/* char: %d */\n\n", i);
  562.             }
  563.  
  564.             for (j = 0, spath = strptr->subpaths;
  565.                  j < strptr->n_subpaths; j++, spath++) {
  566.                 int z;
  567.  
  568.                 printf("static CoordRec char%d_stroke%d[] = {\n", i, j);
  569.                 for(z = 0; z < spath->n_pts; z++) {
  570.                     printf("    { %g, %g },\n",
  571.                         spath->pts.pt2df[z].x, spath->pts.pt2df[z].y);
  572.                 }
  573.                 printf("};\n\n");
  574.             }
  575.  
  576.             printf("static StrokeRec char%d[] = {\n", i);
  577.                         for (j = 0, spath = strptr->subpaths;
  578.                              j < strptr->n_subpaths; j++, spath++) {
  579.                 printf("   { %d, char%d_stroke%d },\n",
  580.                     spath->n_pts, i, j);
  581.             }
  582.             printf("};\n\n");
  583.         }
  584.     }
  585.     printf("static StrokeCharRec chars[] = {\n");
  586.     for (i = 0, tbl_ptr = Table, strptr = strokes;
  587.          i < head.num_ch; i++, tbl_ptr++, strptr++) {
  588.         if (strptr->n_subpaths > 0 &&
  589.         tbl_ptr->center != 0.0 &&
  590.         tbl_ptr->right != 0.0) {
  591.         printf("    { %d, char%d, %g, %g },\n",
  592.             strptr->n_subpaths, i, tbl_ptr->center, tbl_ptr->right);
  593.         } else {
  594.         printf("    { 0, /* char%d */ 0, %g, %g },\n",
  595.             i, tbl_ptr->center, tbl_ptr->right);
  596.         }
  597.     }
  598.     printf("};\n\n");
  599.  
  600.     printf("StrokeFontRec %s = { \"%s\", %d, chars, %.6g, %.6g };\n\n",
  601.         symname, head.name, head.num_ch,
  602.         (double) head.top, (double) head.bottom);
  603.  
  604.     fflush(stdout);
  605.  
  606.     freeall();
  607. #    undef BY_BYE
  608. }
  609.  
  610. freeall()
  611. {
  612.     register Path  *path;
  613.     register Path_subpath *spath;
  614.     register int    i,
  615.                     j,
  616.                     n;
  617.  
  618.     path = strokes;
  619.     for (i = 0; i < head.num_ch; i++, path++) {
  620.         n = path->n_subpaths;
  621.         if (n <= 0)
  622.             continue;
  623.         spath = path->subpaths;
  624.         for (j = 0; j < n; j++, spath++)
  625.             if (spath->pts.pt2df != NULL)
  626.                 free((char *) spath->pts.pt2df);
  627.         if (path->subpaths != NULL)
  628.             free((char *) path->subpaths);
  629.     }
  630.     free(Table);
  631.     free(sp_table);
  632.     free(strokes);
  633.     if (head.properties != NULL)
  634.       free((char *) head.properties);
  635. }
  636.  
  637. check_nstroke()
  638. {
  639.     count.path++;
  640.     if (expect.path != count.path)
  641.         BYE(PATH_MISMATCH);
  642. }
  643.  
  644. check_npts()
  645. {
  646.     count.point++;
  647.     if (expect.point != count.point)
  648.         BYE(POINT_MISMATCH);
  649. }
  650.